﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Newtonsoft.Json; 
using UnityEditor;
using UnityEngine;
using System.Collections;
using Object = UnityEngine.Object;

public class ResourceConfigTool : EditorWindow
{
	private Dictionary<string, Asset> AssetConfig = new Dictionary<string, Asset>();

	private string configFilePath = "Assets/_Gate/Resources/PrefabConfig.txt"; 

#if UNITY_ANDROID
	private string downloadFilePath = "Assets/_Gate/Resources/download_list_android.txt";
#elif  UNITY_STANDALONE_WIN
	private string downloadFilePath = "Assets/_Gate/Resources/download_list_win.txt";
#elif UNITY_IPONE || UNITY_IOS || UNITY_IOS_API
	private string downloadFilePath = "Assets/_Gate/Resources/download_list_ios.txt";
#endif

	private string ResourceFolderKey = "resourcefolder";

	private string AssetBundleKey = "assetbundle";

	private TextAsset ConfigAsset;

	private TextAsset DownloadList;

	private Dictionary<string, AssetList> groupedAssetLists;

	private Dictionary<string, BundleFile> bundleFileDic;

	private bool isDirty;

	private Action onStartPaint;

	private Action onEndPaint;

	private string assetBundleName;

	

	private Vector2 bundleMScroll;

	private bool isOneResOneFile = true;


	private List<BundleFile> _bundleList;

	private List<BundleFile> BundleList
	{
		get
		{
			if (_bundleList == null || _bundleList.Count != bundleFileDic.Count)
			{
				_bundleList = bundleFileDic.Values.ToList();
			}


			SortBundleList(_bundleList);

			return _bundleList;
		}
	}

#if UNITY_ANDROID
	private BuildTarget target = BuildTarget.Android;
#elif  UNITY_STANDALONE_WIN
	private BuildTarget target = BuildTarget.StandaloneWindows;
#elif UNITY_IPONE || UNITY_IOS || UNITY_IOS_API
	private BuildTarget target = BuildTarget.iOS;
#endif
	public BuildTarget Target
	{
		get { return target; }
		set
		{ 
			if (value != target)
			{
				target = value;
				foreach (var bundleFile in bundleFileDic.Values)
				{
					bundleFile.BundleType = value.ToString().ToUpper();
				}
			}
			
			
		}
	}

	public int BundleCount
	{
		get
		{
			return bundleFileDic.Count;
		}
	}

	public int GroupedAssetListCount
	{
		get
		{
			return groupedAssetLists.Count;
		}
	}

	[MenuItem("Coser/Tools/Config Resources")]
	public static void InitWindow()
	{
		EditorWindow.GetWindow<ResourceConfigTool>();
	}

	public ResourceConfigTool()
	{
		groupedAssetLists = new Dictionary<string, AssetList>();
		bundleFileDic = new Dictionary<string, BundleFile>();
		ConfigAsset = AssetDatabase.LoadAssetAtPath(configFilePath, typeof(TextAsset)) as TextAsset;

		DownloadList = AssetDatabase.LoadAssetAtPath(downloadFilePath, typeof(TextAsset)) as TextAsset;
		if (!ConfigAsset)
		{
			Debug.LogError("config File has not been found...pls check it");
		}

		LoadConfig(ConfigAsset.text);

		LoadConfig(DownloadList.text);

		GroupAssets(isOneResOneFile);
	}


	private void OnGUI()
	{
		if (onStartPaint != null)
		{
			onStartPaint();
			onStartPaint = null;
		}

		EditorGUILayout.BeginHorizontal();
		if (GUILayout.Button(BundleCount + " Get download json"))
		{
			//HttpResult<List<BundleFile>> t = new HttpResult<List<BundleFile>>(HttpResult<List<BundleFile>>.ResType.SUCCESS, bundleFileDic.Values.ToList(),"","");
			string t = JsonConvert.SerializeObject(bundleFileDic.Values.ToList());
			string content = "{\"result\":0, \"data\":" + t + "}";
			using (var sw = File.CreateText("Assets/" + Path.GetFileNameWithoutExtension(downloadFilePath)))
			{
				sw.Write(content);
			}

			Debug.Log(content);
		}

		if (GUILayout.Button("One res, One BundleFile"))
		{
			GroupToOneResOneFile();
		}


		if (GUILayout.Button("creat a assetbundle"))
		{
			CreatAssetBundle();
		}
		assetBundleName = EditorGUILayout.TextArea(assetBundleName);

		if (GUILayout.Button("Build all assetbundle"))
		{
			var path = EditorUtility.SaveFolderPanel("Save Resource", "", "AssetBundles");
			if (!string.IsNullOrEmpty(path))
			{
				foreach (KeyValuePair<string, AssetList> keyValuePair in groupedAssetLists)
				{
					if (keyValuePair.Key.Trim().ToLower() != ResourceFolderKey)
					{
						BuildAssetBundle(keyValuePair, path + "/" + keyValuePair.Key + ".unity3d");
					}
				}
			}
			
		}

		if (GUILayout.Button("Set all assetbundle path"))
		{
			var path = EditorUtility.SaveFolderPanel("Save Resource", "", "AssetBundles");
			if (!string.IsNullOrEmpty(path))
			{
				foreach (KeyValuePair<string, AssetList> keyValuePair in groupedAssetLists)
				{
					if (keyValuePair.Key != ResourceFolderKey)
					{
						BuildAssetBundle(keyValuePair, path + "/" + keyValuePair.Key + ".unity3d", true);
					}
				}
			}
			
		}

		if (GUILayout.Button("Add all assetBundle to the download list"))
		{
			foreach (KeyValuePair<string, AssetList> keyValuePair in groupedAssetLists)
			{
				AddToDownloadList(keyValuePair);
			}
		}

		if (GUILayout.Button(GroupedAssetListCount + " Delete all builded assets"))
		{
			foreach (var groupedAssetList in groupedAssetLists)
			{
				if (groupedAssetList.Key != ResourceFolderKey)
				{
					foreach (var asset in groupedAssetList.Value.Assets)
					{
						AssetDatabase.DeleteAsset(asset.FullPath);
					}
				}
			}
		}

		Target = (BuildTarget)Enum.Parse(typeof(BuildTarget), EditorGUILayout.EnumPopup(Target).ToString());

		EditorGUILayout.EndHorizontal();

		bundleMScroll = EditorGUILayout.BeginScrollView(bundleMScroll);
		EditorGUILayout.BeginHorizontal();
		foreach (KeyValuePair<string, AssetList> keyValuePair in groupedAssetLists)
		{
			EditorGUILayout.BeginVertical();

			EditorGUILayout.BeginHorizontal();
			var title = keyValuePair.Value.ListName;

			if (GUILayout.Button("add to " + title, GUILayout.Width(360)))
			{
				AddToCorrespondList(title);
			}
			if (GUILayout.Button("Delete this bundle"))
			{
				DeleteCorrespondList(title);
			}
			keyValuePair.Value.Show = EditorGUILayout.Toggle(keyValuePair.Value.Show);
			EditorGUILayout.EndHorizontal();

			if (keyValuePair.Key != ResourceFolderKey)
			{
				EditorGUILayout.BeginHorizontal();
				if (GUILayout.Button("Build AssetBundle"))
				{ // Bring up save panel
					var path = EditorUtility.SaveFilePanel("Save Resource", "", keyValuePair.Key, "unity3d");
					BuildAssetBundle(keyValuePair, path);
				}
				if (GUILayout.Button("Add to Download list"))
				{
					AddToDownloadList(keyValuePair);
				}

				EditorGUILayout.EndHorizontal();
			}


			keyValuePair.Value.mScroll = EditorGUILayout.BeginScrollView(keyValuePair.Value.mScroll);
			foreach (var asset in keyValuePair.Value.Assets)
			{
				//asset.Show = EditorGUILayout.Toggle(asset.Show);
				DrawAssetInfo(asset);
			}
			EditorGUILayout.EndScrollView();
			EditorGUILayout.EndVertical();
		}

		EditorGUILayout.BeginVertical();
		if (GUILayout.Button(BundleCount + " Delete All"))
		{
			bundleFileDic.Clear();
			isDirty = true;
		}
		foreach (BundleFile bundle in BundleList)
		{
			DrawBundleFileInfo(bundle);
		}
		EditorGUILayout.EndVertical();
		EditorGUILayout.EndHorizontal();
		EditorGUILayout.EndScrollView();
		if (isDirty || GUILayout.Button("Save"))
		{
			GroupAssets(isOneResOneFile);
			isDirty = false;
			SaveToFile();
		}

	}



	#region methods

	#region actions



	private void AddToDownloadList(KeyValuePair<string, AssetList> keyValuePair)
	{
		Debug.Log(bundleFileDic.Count);
		if (!string.IsNullOrEmpty(keyValuePair.Value.BundleSavePath))
		{
			if (bundleFileDic.ContainsKey(keyValuePair.Key))
			{
				//Debug.Log("Alreay has a file named:" + keyValuePair.Key);
				bundleFileDic[keyValuePair.Key].BundleType = Target.ToString().ToUpper();
				bundleFileDic[keyValuePair.Key].FileLength = new FileInfo(keyValuePair.Value.BundleSavePath).Length;
			}
			else
			{
				bundleFileDic.Add(keyValuePair.Key, new BundleFile()
				{
					BundleFileName = keyValuePair.Key,
					Version = 0,
					BundleType = Target.ToString().ToUpper(),
					FileLength = new FileInfo(keyValuePair.Value.BundleSavePath).Length// File.ReadAllBytes(keyValuePair.Value.BundleSavePath).Length
				});
				
			}

			//isDirty = true;
			//TODO:逻辑同步~
			SaveToFile();

		}
		else
		{
			Debug.Log("You need to build the bundle first...");
		}
	}

	private void BuildAssetBundle(KeyValuePair<string, AssetList> keyValuePair, string path, bool justSetPath = false)
	{
		Debug.Log(path);
		keyValuePair.Value.BundleSavePath = path;
		if (path.Length != 0 && keyValuePair.Value.Assets.Count != 0)
		{
			// Build the resource file from the active selection.
			var resources = new List<Object>();
			Type objType = null;
			switch (keyValuePair.Value.Assets.First().AssetType)
			{
				case "GameObject":
					objType = typeof(GameObject);
					break;
				case "AudioClip":
					objType = typeof(AudioClip);
					break;
				case "Texture2D":
					objType = typeof(Texture2D);
					break;
				case "TextAsset":
					objType = typeof(TextAsset);
					break;
				default:
					objType = typeof(GameObject);
					break;
			}
			foreach (var resource in keyValuePair.Value.Assets)
			{
				Debug.Log("Asset Full Path:" + resource.FullPath);
				var obj = AssetDatabase.LoadAssetAtPath(resource.FullPath, objType);
				if (obj)
				{
					resources.Add(obj);
				}
			}
			if (!justSetPath)
			{ 
				if (resources.Count > 0)
				{
					BuildPipeline.BuildAssetBundle(resources.First(), resources.ToArray(), path,
							BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets |
							BuildAssetBundleOptions.DisableWriteTypeTree, Target);
				}
				else
				{
					Debug.LogError("res did not exist at:" + keyValuePair.Key + "/" + keyValuePair.Value.ToString());
				}

			}
		}
	}



	private void AddToCorrespondList(string title)
	{
		// Build the resource file from the active selection.
		Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
		if (selection.Length == 0)
		{
			Debug.Log("you need to select some assets to add..");
		}
		else
		{
			foreach (var o in selection)
			{
				AddObjectToCorrespondList(title, o);
			}
			isDirty = true;
		}
	}

	private void AddObjectToCorrespondList(string title, Object o)
	{
		var fullPath = AssetDatabase.GetAssetPath(o);
		var path = GetResourcePath(fullPath);
		if (AssetConfig.ContainsKey(o.name))
		{
			Debug.LogWarning("pls check,already has a key named: " + o.name);
		}
		else
		{
			AssetConfig.Add(o.name, new Asset()
			{
				ShortName = o.name.StartsWith("UI_") ? o.name.Replace("UI_", "") : o.name,
				AssetName = o.name,
				ParentFolderPath = path,
				AssetBundleFileName = title.Trim().ToLower() == ResourceFolderKey ? string.Empty : title,
				AssetType = o.GetType().ToString().Split('.').Last(),
				ObtainMethod = title.Trim().ToLower() == ResourceFolderKey ? ResourceFolderKey : AssetBundleKey,
				Version = 0,
				Show = true,
				FullPath = fullPath,
				BuildType = o.GetType()
			});
		}
	}

	private void CreatAssetBundle()
	{
		if (!string.IsNullOrEmpty(assetBundleName))
		{
			if (groupedAssetLists.ContainsKey(assetBundleName))
			{
				Debug.Log("already has a assetbundle named: " + assetBundleName);
			}
			else
			{
				groupedAssetLists.Add(assetBundleName, new AssetList()
				{
					Assets = new List<Asset>(),
					ListName = assetBundleName
				});
			}
		}
		else
		{
			// 将选中的当作mainAssets
			// Build the resource file from the active selection.
			Object selection = Selection.GetFiltered(typeof(Object), SelectionMode.Assets).First();
			if (selection)
			{
				var bundlename = selection.name;
				if (groupedAssetLists.ContainsKey(bundlename))
				{
					Debug.Log("already has a assetbundle named: " + bundlename);
				}
				else
				{
					groupedAssetLists.Add(bundlename, new AssetList()
					{
						Assets = new List<Asset>(),
						ListName = bundlename
					});
				}

				AddObjectToCorrespondList(bundlename, selection);
				isDirty = true;
			}
			Debug.Log("input a assetbundle name...");
		}
	}


	private void DeleteCorrespondList(string title)
	{
		AssetConfig = AssetConfig.Where(keyValuePair => keyValuePair.Value.AssetBundleFileName != title).ToArray().ToDictionary(t => t.Key, t => t.Value);

		isDirty = true;
	}

	private void AddFolderPrefab()
	{
		Object[] selections = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
		foreach (Object selection in selections)
		{
			if (selection)
			{
				var bundlename = selection.name;
				if (groupedAssetLists.ContainsKey(bundlename))
				{
					Debug.Log("already has a assetbundle named: " + bundlename);
				}
				else
				{
					groupedAssetLists.Add(bundlename, new AssetList()
					{
						Assets = new List<Asset>(),
						ListName = bundlename
					});
				}

				AddObjectToCorrespondList(bundlename, selection);
				isDirty = true;
			}
			Debug.Log("input a assetbundle name...");
		}

	}

	#endregion

	#region help methods
	private void GroupAssets(bool isOneResOneFile = false)
	{
		if (isOneResOneFile)
		{
			GroupToOneResOneFile();
		}
		else
		{
			groupedAssetLists.Clear();
			SyncAssetListContent(ResourceFolderKey, new AssetList()
			{
				Assets = AssetConfig.Values.Where(t => t.ObtainMethod.Trim().ToLower() == ResourceFolderKey).ToArray().ToList(),
				ListName = ResourceFolderKey
			});

			var bundleAssetList =
				AssetConfig.Values.Where(t => t.ObtainMethod.Trim().ToLower() == AssetBundleKey).GroupBy(t => t.AssetBundleFileName).ToArray().ToDictionary(t => t.Key);


			foreach (KeyValuePair<string, IGrouping<string, Asset>> keyValuePair in bundleAssetList)
			{
				SyncAssetListContent(keyValuePair.Key, new AssetList()
				{
					Assets = keyValuePair.Value.ToList(),
					ListName = keyValuePair.Key
				});

			}
		}
	}

	private void GroupToOneResOneFile()
	{
		groupedAssetLists.Clear();
		SyncAssetListContent(ResourceFolderKey, new AssetList()
		{
			Assets = AssetConfig.Values.Where(t => t.ObtainMethod.Trim().ToLower() == ResourceFolderKey).ToArray().ToList(),
			ListName = ResourceFolderKey
		});
		int inx = 0;
		foreach (KeyValuePair<string, Asset> keyValuePair in AssetConfig)
		{
			if (keyValuePair.Value.ObtainMethod.Trim().ToLower() != ResourceFolderKey)
			{
				var bundleName = inx.ToString();
				keyValuePair.Value.AssetBundleFileName = bundleName;
				groupedAssetLists.Add(bundleName, new AssetList()
				{
					Assets = new List<Asset>() { keyValuePair.Value },
					ListName = bundleName,
				});
				inx++;
			}
			
		}
	}

	private int TextAreaMinWidth = 150;
	private int LableFieldMaxWidth = 80;
	private void DrawAssetInfo(Asset asset)
	{
		if (asset.Show)
		{
			EditorGUILayout.BeginHorizontal();
			if (GUILayout.Button(asset.AssetName, GUILayout.Width(100), GUILayout.Height(124)))
			{
				// onEndPaint = () =>
				// {

				AssetConfig.Remove(asset.AssetName);

				isDirty = true;
				// }; 
			}

			//EditorGUILayout.BeginVertical();
			//EditorGUILayout.BeginHorizontal();
			//EditorGUILayout.LabelField("ShortName:", GUILayout.MaxWidth(LableFieldMaxWidth));
			//asset.ShortName = EditorGUILayout.TextArea(asset.ShortName, GUILayout.MinWidth(TextAreaMinWidth));
			//EditorGUILayout.EndHorizontal();
			//EditorGUILayout.BeginHorizontal();
			//EditorGUILayout.LabelField("AssetName:", GUILayout.MaxWidth(LableFieldMaxWidth));
			//asset.AssetName = EditorGUILayout.TextArea(asset.AssetName, GUILayout.MinWidth(TextAreaMinWidth));
			//EditorGUILayout.EndHorizontal();
			//EditorGUILayout.BeginHorizontal();
			//EditorGUILayout.LabelField("AssetType:", GUILayout.MaxWidth(LableFieldMaxWidth));
			//asset.AssetType = EditorGUILayout.TextArea(asset.AssetType, GUILayout.MinWidth(TextAreaMinWidth));
			//EditorGUILayout.EndHorizontal();
			//EditorGUILayout.BeginHorizontal();
			//EditorGUILayout.LabelField("ObtainMethod:", GUILayout.MaxWidth(LableFieldMaxWidth));
			//asset.ObtainMethod = EditorGUILayout.TextArea(asset.ObtainMethod, GUILayout.MinWidth(TextAreaMinWidth));
			//EditorGUILayout.EndHorizontal();
			//EditorGUILayout.BeginHorizontal();
			//EditorGUILayout.LabelField("ParentFolderPath:", GUILayout.MaxWidth(LableFieldMaxWidth));
			//asset.ParentFolderPath = EditorGUILayout.TextArea(asset.ParentFolderPath, GUILayout.MinWidth(TextAreaMinWidth));
			//EditorGUILayout.EndHorizontal();
			//EditorGUILayout.BeginHorizontal();
			//EditorGUILayout.LabelField("AssetBundleFileName:", GUILayout.MaxWidth(LableFieldMaxWidth));
			//asset.AssetBundleFileName = EditorGUILayout.TextArea(asset.AssetBundleFileName, GUILayout.MinWidth(TextAreaMinWidth));
			//EditorGUILayout.EndHorizontal();
			//EditorGUILayout.BeginHorizontal();
			//EditorGUILayout.LabelField("Version:", GUILayout.MaxWidth(LableFieldMaxWidth));
			//asset.Version = int.Parse(EditorGUILayout.TextArea(asset.Version.ToString(), GUILayout.MinWidth(TextAreaMinWidth)));
			//EditorGUILayout.EndHorizontal();
			//EditorGUILayout.EndVertical();

			EditorGUILayout.EndHorizontal();
		}
		else
		{
			EditorGUILayout.LabelField(asset.AssetName);
		}

	}

	private void DrawBundleFileInfo(BundleFile bundle)
	{
		EditorGUILayout.BeginHorizontal();
		if (GUILayout.Button(bundle.BundleFileName, GUILayout.Width(80), GUILayout.Height(90)))
		{
			bundleFileDic.Remove(bundle.BundleFileName);

			isDirty = true;
		}

		EditorGUILayout.BeginVertical();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("BundleFileName:", GUILayout.MaxWidth(LableFieldMaxWidth));
		EditorGUILayout.LabelField(bundle.BundleFileName, GUILayout.MinWidth(TextAreaMinWidth));
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("Version:", GUILayout.MaxWidth(LableFieldMaxWidth));
		bundle.Version = int.Parse(EditorGUILayout.TextArea(bundle.Version.ToString(), GUILayout.MinWidth(TextAreaMinWidth)));
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("FileLength:", GUILayout.MaxWidth(LableFieldMaxWidth));
		EditorGUILayout.LabelField(bundle.FileLength.ToString(), GUILayout.MinWidth(TextAreaMinWidth));
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("BundleType:", GUILayout.MaxWidth(LableFieldMaxWidth));
		EditorGUILayout.LabelField(bundle.BundleType, GUILayout.MinWidth(TextAreaMinWidth));
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.BeginHorizontal();
		EditorGUILayout.LabelField("DownloadOrder:", GUILayout.MaxWidth(LableFieldMaxWidth));
		bundle.DownloadOrder = int.Parse(EditorGUILayout.TextArea(bundle.DownloadOrder.ToString(), GUILayout.MinWidth(TextAreaMinWidth)));

		EditorGUILayout.EndHorizontal();

		EditorGUILayout.EndVertical();


		EditorGUILayout.EndHorizontal();
	}

	private void LoadConfig(string text)
	{
		string[] pairs = text.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
		foreach (var pair in pairs)
		{

			if (pair.StartsWith("#"))
			{
				continue;
			}
			var strs = pair.Split(',');
			//Prefab txt
			if (strs.Length == 8)
			{
				if (AssetConfig.ContainsKey((strs[0].Trim())))
				{
					Debug.LogError("Prefab path dictionary alreay has a key named:" + strs[0]);
				}
				else
				{
					AssetConfig.Add((strs[1].Trim()), new Asset()
					{
						ShortName = strs[0].Trim(),
						AssetName = strs[1].Trim(),
						AssetType = strs[2].Trim(),
						ObtainMethod = strs[3].Trim(),
						ParentFolderPath = strs[4].Trim(),
						AssetBundleFileName = strs[5].Trim(),
						Version = int.Parse(strs[6].Trim()),
						FullPath = strs[7].Trim(),
						Show = true
					});
				}
			}
			// Download List txt
			if (strs.Length == 6)
			{
				if (bundleFileDic.ContainsKey((strs[0].Trim())))
				{
					Debug.LogError("download list dictionary alreay has a key named:" + strs[0]);
				}
				else
				{
					bundleFileDic.Add((strs[0].Trim()), new BundleFile()
					{
						BundleFileName = strs[0].Trim(),
						Version = int.Parse(strs[1].Trim()),
						FileLength = int.Parse(strs[2].Trim()),
						BundleType = strs[3].Trim(),
						DownloadOrder = int.Parse(strs[4].Trim())
					});
				}
			}
		}
	}

	private void SaveToFile()
	{
		using (StreamWriter sw = new StreamWriter(configFilePath, false, Encoding.UTF8))
		{
			foreach (var assetInfo in AssetConfig.Values)
			{
				sw.WriteLine(assetInfo.ToString());
			}
		}

		using (StreamWriter sw = new StreamWriter(downloadFilePath, false, Encoding.UTF8))
		{
			var lists = bundleFileDic.Values.ToList();
			Debug.Log("Save count " + lists.Count);
			SortBundleList(lists);

			foreach (BundleFile bundleFile in lists)
			{
				sw.WriteLine(bundleFile.ToString());
			}
		}
	}

	private static void SortBundleList(List<BundleFile> lists)
	{
		lists.Sort((a, b) =>
		{
			if (a.DownloadOrder == b.DownloadOrder)
			{
				return 1;
			}
			else
			{
				return a.DownloadOrder.CompareTo(b.DownloadOrder);
			}
		});
	}

	private string GetResourcePath(string path)
	{
		var strs = path.Split('/');
		var resPath = string.Empty;
		for (int i = strs.Length - 2; i > 0; i--)
		{
			if (strs[i].Trim().ToLower() == "resources")
			{
				break;
			}
			else
			{
				resPath = strs[i] + "/" + resPath;
			}
		}
		return resPath;
	}

	private void SyncAssetListContent(string key, AssetList list)
	{
		Debug.Log("SyncAssetListContent:" + key + "/" + list.Assets.Count);
		if (groupedAssetLists.ContainsKey(key))
		{
			groupedAssetLists[key] = (list);
		}
		else
		{
			groupedAssetLists.Add(key, list);
		}
	}

	#endregion

	#endregion

	#region help class
	private class BundleFile
	{
		public string BundleFileName;

		public int Version;

		public long FileLength;

		/// <summary>
		/// IOS,ANDROID
		/// </summary>
		public string BundleType;

		public int DownloadOrder;

		public override string ToString()
		{
			return BundleFileName + "," + Version + "," + FileLength + "," + BundleType + "," + DownloadOrder + "," + "false";
		}
	}


	/// <summary>
	/// Asset 基本单元 TODO:Asset Bundle resource
	/// </summary>
	public class Asset
	{
		/// <summary>
		/// 窗体Prefab枚举名字
		/// </summary>
		public string ShortName;

		/// <summary>
		/// 在ProjectView 中的名字
		/// </summary>
		public string AssetName;

		/// <summary>
		/// GameObject,Texture2D,AudioClip...
		/// </summary>
		public string AssetType;

		/// <summary>
		/// 获取方式（ResourceFolder 和 AssetBundle）
		/// </summary>
		public string ObtainMethod;

		/// <summary>
		/// 该Asset对象的文件的父文件夹的相对路径
		/// </summary>
		public string ParentFolderPath;

		/// <summary>
		/// AssetBundle文件的名字
		/// </summary>
		public string AssetBundleFileName;

		public int Version;

		public bool Show;

		public string FullPath;

		public Type BuildType;

		public override string ToString()
		{
			return ShortName + "," + AssetName + "," + AssetType + "," + ObtainMethod + "," + ParentFolderPath + "," +
				   AssetBundleFileName + "," + Version + "," + FullPath;
		}
	}

	private class AssetList
	{
		public List<Asset> Assets;

		public string ListName;

		public Vector2 mScroll;

		private bool _show = true;
		public bool Show
		{
			get { return _show; }
			set
			{
				_show = value;
				foreach (Asset asset in Assets)
				{
					asset.Show = value;
				}
			}
		}

		public string BundleSavePath;

		public void AddRangeToAssets(AssetList list)
		{
			foreach (var asset in list.Assets)
			{
				if (!Assets.Contains(asset))
				{
					Assets.Add(asset);
				}
			}
		}
	}

	#endregion
}
